<?php
/**
 * User: xuanbang.wen
 * Date: 2019/12/25
 */

namespace App\Command\Syncdata;


use App\Web\Model\Project;
use EchoPHP\Console\Command;
use EchoPHP\Facades\Db;

class ProjectCrmInfoSync extends Command {
    protected $signature = "syncdata:projectCrmInfoSync";
    protected $description = 'crm相关信息同步至Project表';

    public function __construct() {
        parent::__construct();
    }

    public function handle() {
        $sql = 'select projectid from Project where crmprojectnum != ? and crmstartdate is null';
        $projectIds = Db::select($sql, ['']);
        if (!$projectIds) {
            $this->output->success('没有项目需要同步');
            die();
        }
        $projectIds = array_column($projectIds, 'projectid');

        $projectModel = new Project();
        $errorList = $successIds = [];
        foreach ($projectIds as $projectId) {
            $tmp = $projectModel->crmInfoSync($projectId);
            if ($tmp['status'] == false) {
                $errorList [] = '项目id：' . $projectId . '同步失败，' . $tmp['message'];
            } else {
                $successIds [] = $projectId;
            }
        }
        $successStr = $successIds ? implode(',', $successIds) : '无';
        $errorStr = $errorList ? implode(',', $errorList) : '无';
        $massage = '项目crm信息同步完成。同步成功项目：' . $successStr . '。失败项目：' . $errorStr . '。';
        $this->output->success($massage);
        exit();
    }

}